package com.zzl.java8.czbk.stream.test1;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * description: Stream API之中间操作filter过滤（无状态）
 * @author 朱正磊
 * @date 2022-06-20 17:00:05
 */
public class StreamApiTest2 {

    @Test
    void test1() {
        // 创建collection集合
        List<PbCourse> list = new ArrayList<>();
        list.add(new PbCourse(100L, "攀博课堂-Java面向对象教程", 33));
        list.add(new PbCourse(101L, "攀博课堂-Spring全家桶教程", 44));
        list.add(new PbCourse(102L, "攀博课堂-SpringCloud微服务教程", 55));
        list.add(new PbCourse(103L, "攀博课堂-在线教育分布式项目教程", 66));
        list.add(new PbCourse(104L, "攀博课堂-电子商务项目教程", 77));
        // 调用filter方法获取价格大于50元的课程
        Stream<PbCourse> pbCourseStream = list.stream().filter(x -> x.getPrice() > 50);
        pbCourseStream.forEach(System.out::println);

        // 代码简化
        list.stream().filter(x -> x.getPrice() > 50).forEach(System.out::println);
    }

    @Test
    void test2() {
        // 创建collection集合
        List<PbCourse> list = new ArrayList<>();
        list.add(new PbCourse(100L, "攀博课堂-Java面向对象教程", 33));
        list.add(new PbCourse(101L, "攀博课堂-Spring全家桶教程", 44));
        list.add(new PbCourse(102L, "攀博课堂-SpringCloud微服务教程", 55));
        list.add(new PbCourse(103L, "攀博课堂-在线教育分布式项目教程", 66));
        list.add(new PbCourse(104L, "攀博课堂-电子商务项目教程", 77));

        list.stream().filter(x -> x.getPrice() > 50 && x.getCourseName().contains("Spring")).forEach(System.out::println);

        list.stream().filter(x -> x.getPrice() > 50).filter(x -> x.getCourseName().contains("Spring")).forEach(System.out::println);
    }

}
